package com.example.comm.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 老王 on 2018/3/1.
 * 时间工具类
 */
public class DateUtils {

    public static final long WEEKTIME = 604800017L;
    public static final long MONTHTIME = 2629800000L;

    /**
     * 功能描述:〈根据时间戳 获取其当天凌晨零时零分的毫秒级〉
     * @param timestamp
     * @return : long
     * @author : 沈明可
     * @date : 2018/12/13 14:44
     */
    public static long getZero(Timestamp timestamp){
        long time = timestamp.getTime();
        long zero = time / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
        return zero;
    }

    /**
     * 功能描述:〈根据计划分钟获取对应的当天时间〉
     * @param zero 当天的零时零分的毫秒级
     * @param planMinute 计划开始分钟数
     * @return : java.lang.String
     * @author : 沈明可
     * @date : 2018/12/13 14:45
     */
    public static String getPlanTime(long zero,Integer planMinute){
        int i = planMinute * 60 * 1000;
        zero+=i;
        return TimestampToString_3(new Timestamp(zero));
    }

    /**
     * 获取当前时间多久前的时间
     * @param flag 如果为1是一周前,2是一月前
     * @return
     * @author 小龙
     */
    public static String getTimeBefore(int flag){
        switch (flag){
            case 1 :
                return DateUtils.TimestampToString_3(new Timestamp(new Date().getTime() - DateUtils.WEEKTIME));
            case 2 :
                return DateUtils.TimestampToString_3(new Timestamp(new Date().getTime() - DateUtils.MONTHTIME));
            default:
                break;
        }
        return null;
    }

    /**
     * 格林时间(中国标准格式)转换为yyyy-MM-dd
     * @param datdString
     * @return
     */
    public static String parseTime(String datdString) {
        datdString = datdString.replace("GMT", "").replaceAll("\\(.*\\)", "");
        //将字符串转化为date类型，格式2016-10-12
        SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss", Locale.ENGLISH);
        Date dateTrans = null;
        try {
            dateTrans = format.parse(datdString);
            return new SimpleDateFormat("yyyy-MM-dd").format(dateTrans);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datdString;
    }

    /**
     * Timestamp格式化显示 yyyy-MM-dd HH:mm
     */
    public static String TimestampToString_1(Timestamp timestamp){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return df.format(timestamp);
    }

    /**
     * Timestamp格式化显示 yyyy-MM-dd HH:mm:ss
     */
    public static String TimestampToString_3(Timestamp timestamp){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(timestamp);
    }


    /**
     * Timestamp格式化显示 yyyy-MM-dd
     */
    public static String TimestampToString_2(Timestamp timestamp){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(timestamp);
    }

    /***
     *
     * @param date
     * @param dateFormat : e.g:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDateByPattern(Date date, String dateFormat){
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }

    /***
     * 把date转成cron表达式
     * @param date  : 时间点
     * @return
     */
    public static String getCron(Date  date){
        String dateFormat="ss mm HH dd MM ? yyyy";
        return formatDateByPattern(date, dateFormat);
    }
    
    /***
     * 把timestamp转成cron表达式
     * @param tm  : 时间点
     * @return
     */
    public static String getCron(Timestamp tm){
        String dateFormat="ss mm HH dd MM ? yyyy";
        Date d = new Date(tm.getTime());
        return formatDateByPattern(d, dateFormat);
    }

    /***
     * 时间戳转换为date
     * @return
     */
    public static Date getDateTime(Timestamp timestamp){
        return new Date(timestamp.getTime());
    }

    /***
     * date转时间戳
     * @return
     */
    public static Date getTimestamp(Date date){
        return new Timestamp(date.getTime());
    }

    /***
     * String转时间戳
     * @return
     */
    public static Timestamp StringToTimestamp(String date){
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        try {
            ts = Timestamp.valueOf(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ts;
    }

    /***
     * String转date
     * @return
     */
    public static Date StringToDate(String date) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(date);
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 
     * @author 胡金鹏
     */
    public static boolean isEffectiveDate(Timestamp nowTime, Timestamp startTime, Timestamp endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /***
     * 获取当天开始时间
     * @return
     */
    public static Date getStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /***
     * 获取当天结束时间
     * @return
     */
    public static Date getEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    //当月第一天
    public static Date getMonthBegin(){
        Calendar calendar1=Calendar.getInstance();
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MILLISECOND, 0);
        return calendar1.getTime();
    }

    //当月最后一天
    public static Date getMonthEnd(){
        Calendar calendar2=Calendar.getInstance();
        Integer maxCurrentMonthDay=calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar2.set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
        calendar2.set(Calendar.HOUR_OF_DAY,23);
        calendar2.set(Calendar.MINUTE, 59);
        calendar2.set(Calendar.SECOND, 59);
        calendar2.set(Calendar.MILLISECOND, 999);
        return calendar2.getTime();
    }

    //前月第一天
    public static Date getMonthBegin_1(){
        Calendar calendar1=Calendar.getInstance();
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MILLISECOND, 0);
        calendar1.add(Calendar.MONTH, -1);
        return calendar1.getTime();
    }

    //前月最后一天
    public static Date getMonthEnd_1(){
        Calendar calendar2=Calendar.getInstance();
        Integer maxCurrentMonthDay=calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar2.set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
        calendar2.set(Calendar.HOUR_OF_DAY,23);
        calendar2.set(Calendar.MINUTE, 59);
        calendar2.set(Calendar.SECOND, 59);
        calendar2.set(Calendar.MILLISECOND, 999);
        calendar2.add(Calendar.MONTH, -1);
        return calendar2.getTime();
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.TimestampToString_3(new Timestamp(DateUtils.getMonthBegin_1().getTime())));
        System.out.println(DateUtils.TimestampToString_3(new Timestamp(DateUtils.getMonthEnd_1().getTime())));
    }

    //获取明天的开始时间
    public static Date getBeginDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    //获取明天的结束时间
    public static Date getEndDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    //获取昨天的开始时间
    public static Date getBeginDayOfYesterday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    //获取昨天的结束时间
    public static Date getEndDayOfYesterday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
    //获取当天的开始时间
    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    //获取当天的结束时间
    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    //把分钟转换成天时分
    public static String getDayHourMinTime(Integer min){
        String s = "";
        s = s + min/60/24 +"天";
        s = s + min/60%24 +"时";
        s = s + min%60 +"分";
        return s;
    }

    //获取明天的开始时间
    public static Date getBeginDayOfYestertoday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    //获取明天的结束时间
    public static Date getEndDayOfYestertoday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
    /**
     * 重新规划时间，重组时间
     * @param timestamp 当天的时间
     * */
    public static Timestamp recombination(Timestamp timestamp,Integer day,Integer time){
        Date date = new Date(timestamp.getTime());
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DAY_OF_MONTH,now.get(Calendar.HOUR_OF_DAY)+day/60/24);
        now.set(Calendar.HOUR_OF_DAY,time/60%24);
        now.set(Calendar.MINUTE,time%60 );
        now.set(Calendar.SECOND,0);
        return new Timestamp(now.getTimeInMillis());
    }
    /**
     * 两个时间戳减得到时间
     * */
    public static String getrealtime(Timestamp timestamp1,Timestamp timestamp2){
        Date d1 = new Date(timestamp1.getTime());
        Date d2 = new Date(timestamp2.getTime());
        long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
        long days = diff / (1000 * 60 * 60 * 24);

        long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
        long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
        return ""+days+"天"+hours+"小时"+minutes+"分";
    }

    /***
     * 获取当天开始时间
     * @return
     */
    public static Date getStartTime(Date date) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /***
     * 获取当天结束时间
     * @return
     */
    public static Date getEndTime(Date date) {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTime(date);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    //当月第一天
    public static Date getMonthBegin(Date date){
        Calendar calendar1=Calendar.getInstance();
        calendar1.setTime(date);
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MILLISECOND, 0);
        return calendar1.getTime();
    }

    //当月最后一天
    public static Date getMonthEnd(Date date){
        Calendar calendar2=Calendar.getInstance();
        calendar2.setTime(date);
        Integer maxCurrentMonthDay=calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar2.set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
        calendar2.set(Calendar.HOUR_OF_DAY,23);
        calendar2.set(Calendar.MINUTE, 59);
        calendar2.set(Calendar.SECOND, 59);
        calendar2.set(Calendar.MILLISECOND, 999);
        return calendar2.getTime();
    }



}
